package com.sghj.utils.Validate.phone;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author xrx
 * @Date 2022/3/7
 * @apiNote
 */
public class PhoneValidator implements ConstraintValidator<Phone,String> {
    private static final String MOBILE_PATTERN = "^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))(\\d{8})$";

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (value==null){
            return true;
        }

        if (StringUtils.isBlank(value))
        {
            return true;
        }
        return isPhone(value);
    }

    private boolean isPhone(String value) {
        Pattern pattern = Pattern.compile(MOBILE_PATTERN);
        Matcher matcher = pattern.matcher(value);
        boolean matches = matcher.matches();
        return matches;
    }
}
